import codecs
from textrank4zh import TextRank4Keyword, TextRank4Sentence

# def textRankWords(strt):
#     dict = './static/dict.txt'
#     fileName = './static/stopwords.txt'
#     jieba.load_userdict(dict)
#     strt = ''.join(strt)
#     words = []
#     with open(fileName, 'r', encoding='utf-8') as fileObject:
#         stopWords = fileObject.readlines()
#         for k in jieba.analyse.textrank(strt, topK=20, withWeight=False, allowPOS=('ns', 'n', 'vn', 'n')):
#             if not k[0].isdigit() and not k[0] in stopWords:
#                 words.append(k[0])
#         return words


def textRank(filename, num):
    text = codecs.open(filename, 'r', 'utf-8').read()
    tr4w = TextRank4Keyword(stop_words_file='./static/stopwords.txt')
    tr4w.analyze(text=text, lower=True, window=2)
    tr4s = TextRank4Sentence(stop_words_file='./static/stopwords.txt')
    tr4s.analyze(text=text, lower=True, source='all_filters')
    words = []
    sentence = []
    for item in tr4w.get_keywords(2*num, word_min_len=1):
        words.append(item.word)
    for item in tr4s.get_key_sentences(num=num):
        sentence.append(item.sentence)
    return words, sentence


